<template>
  <div class="container">
    <div class="main-content">
      <div class="left-video-play">
        <el-card class="left-video-play-card">
          <div class="video-title">{{ nowItem.title }}</div>
          <div class="video-info">
            <el-link icon="el-icon-view">{{ nowItem.view }}次播放</el-link>
            <el-link icon="el-icon-timer" style="margin-left: 15px">{{ nowItem.createTime }}</el-link>
          </div>
          <video-player
              class="video-player vjs-custom-skin video-area"
              ref="videoPlayer"
              :playsinline="true"
              :options="videoPlayerOptions"/>
        </el-card>
        <el-card class="left-video-content-card">
          <div class="content-title">视频内容</div>
          <p>{{ nowItem.introduction }}</p>
        </el-card>
      </div>
      <div class="blank"></div>
      <div class="right-info">
        <el-card class="publisher-info">
          <img :src="nowItem.avatar" alt="" class="publisher-avatar"/>
          <div class="publisher">
            <div class="publisher-name">{{ nowItem.username }}</div>
            <div class="publisher-other-info">
              <div class="publisher-other-info-detail">
                所属医院：{{ nowItem.hospital === null ? '暂无医院' : nowItem.hospital }}
              </div>
              <div class="publisher-other-info-detail" style="margin-left: 5px">
                联系方式：{{ nowItem.phone === null ? '暂无手机号' : nowItem.phone }}
              </div>
            </div>
            <el-button size="mini" class="leave-message-btn" @click="startLeaveMessage">留言</el-button>
          </div>
        </el-card>
        <el-card class="recommend-video">
          <div class="single-recommend-video" v-for="(item,index) in recommendVideo" :key="index"
               @click="toVideoDetail(item)">
            <img :src="item.cover" alt="" class="recommend-video-cover"/>
            <div class="recommend-video-info">
              <div class="recommend-video-title">{{ item.title }}</div>
              <div class="recommend-video-detail">
                <div class="recommend-video-other-info">{{ item.view }}次播放</div>
                <div class="recommend-video-other-info" style="margin-left: 5px">{{ item.createTime }}</div>
              </div>
              <div class="recommend-video-publisher">{{ item.username }}</div>
            </div>
          </div>
        </el-card>
      </div>
    </div>

    <el-dialog
        title="私信留言"
        :visible.sync="showPrivateChatDialog"
        width="30%">
      <el-form v-model="privateMessageForm" label-width="auto">
        <el-form-item label="留言内容">
          <el-input
              resize="none"
              type="textarea"
              :rows="2"
              placeholder="请输入留言内容"
              v-model="privateMessageForm.content">
          </el-input>
        </el-form-item>
        <el-form-item label="附件">
          <el-upload
              drag
              action="https://yiyuy.mynatapp.cc/info/imageToOSS"
              :on-success="handlePrivateMessageUploadSuccess"
              :limit="1">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="showPrivateChatDialog = false">取 消</el-button>
        <el-button type="primary" @click="submitPrivateMessage">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {getRecommendVideo} from "@/api/video";
import moment from "moment";
import {leaveMessage} from "@/api/question";

export default {
  name: 'videoPlayerOptions',
  data() {
    return {
      // 是否显示私信留言窗口
      showPrivateChatDialog: false,
      // 私信留言需要填写的表格
      privateMessageForm: {
        content: '',
        fileUrl: '',
        toUsername: '',
      },
      nowItem: null,
      recommendVideo: [],
      // 视频播放配置
      videoPlayerOptions: {
        playbackRates: [0.7, 1.0, 1.5, 2.0], // 播放速度
        autoplay: false, //如果true,浏览器准备好时开始回放。
        muted: false, // 默认情况下将会消除任何音频。
        loop: false, // 导致视频一结束就重新开始。
        preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
        language: 'zh-CN',
        aspectRatio: '16:9', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
        fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        sources: [{
          type: "video/mp4",// 这里的种类支持很多种：基本视频格式、直播、流媒体等，具体可以参看git网址项目
          src: "/api/project/getPlayResource/25" // url地址
        }],
        poster: "https://p1.music.126.net/5zs7IvmLv7KahY3BFzUmrg==/109951163635241613.jpg?param=600y500", // 你的封面地址
        notSupportedMessage: '此视频暂无法播放，请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true,
          durationDisplay: true,
          remainingTimeDisplay: false,
          fullscreenToggle: true  // 全屏按钮
        }
      }
    }
  },
  mounted() {
    this.nowItem = this.$route.query.item
    this.videoPlayerOptions.poster = this.nowItem.cover
    this.videoPlayerOptions.sources[0].src = this.nowItem.videoSrc
    this.getRelatedVideo();
  },
  methods: {
    startLeaveMessage() {
      this.showPrivateChatDialog = true;
      this.privateMessageForm = {
        content: '',
        fileUrl: '',
        toUsername: this.nowItem.username,
      }
    },
    handlePrivateMessageUploadSuccess(response, file, fileList) {
      this.privateMessageForm.fileUrl = response.data;
      this.$message.success('文件上传成功')
    },
    getRelatedVideo() {
      getRecommendVideo(this.nowItem.id).then(res => {
        this.recommendVideo = res.data
        for (let i = 0; i < this.recommendVideo.length; i++) {
          this.recommendVideo[i].createTime = moment(this.recommendVideo[i].createTime).format('YYYY-MM-DD')
        }
      })
    },
    toVideoDetail(item) {
      this.$router.push({path: '/home/videoDetail', query: {item: item}})
    },
    submitPrivateMessage() {
      leaveMessage(this.privateMessageForm).then(res => {
        this.$message.success('留言成功')
        this.showPrivateChatDialog = false;
      })
    }
  }
}
</script>

<style lang="less" scoped>
@keyframes MoveIn {
  0% {
    opacity: 0;
    transform: translateY(12px);
  }

  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

.container {
  width: 100%;
  height: 100%;

  .main-content {
    width: 100%;
    height: 100%;
    display: flex;
    animation: MoveIn .7s cubic-bezier(.4, 0, .2, 1);

    .left-video-play {
      width: 70%;
      height: 100%;
      display: flex;
      flex-direction: column;

      .left-video-play-card {
        .video-title {
          font-size: 22px;
          font-weight: 700;
        }

        .video-info {
          margin-top: 5px;
        }

        .video-area {
          margin-top: 10px;
          width: 100%;
        }
      }

      .left-video-content-card {
        margin-top: 5px;

        .content-title {
          font-size: 18px;
          font-weight: 700;
        }
      }
    }

    .blank {
      width: 20px;
    }

    .right-info {
      flex: 1;
      height: 100%;
      display: flex;
      flex-direction: column;

      .publisher-info {
        width: 100%;

        /deep/ .el-card__body {
          width: 100%;
          display: flex;
          align-items: center;
        }

        .publisher-avatar {
          width: 60px;
          height: 60px;
          border-radius: 50%;
        }

        .publisher {
          flex: 1;
          margin-left: 10px;

          .publisher-name {
            color: #00a1d6;
            font-size: 20px;
          }

          .publisher-other-info {
            display: flex;
            margin-top: 5px;

            .publisher-other-info-detail {
              font-size: 13px;
              color: #9499a0;
            }
          }

          .leave-message-btn {
            width: 50%;
            margin-top: 5px;
            background-color: rgba(0, 161, 214, 0.8);
            color: #ffffff;
          }
        }
      }

      .recommend-video {
        margin-top: 5px;

        .single-recommend-video {
          margin-top: 10px;
          display: flex;
          align-items: center;
          border-bottom: 1px solid #eee;
          cursor: pointer;

          .recommend-video-cover {
            width: 200px;
            height: 100px;
          }

          .recommend-video-info {
            margin-left: 10px;

            .recommend-video-title {
              font-weight: 700;
              white-space: nowrap; /* 确保文本在一行内显示 */
              overflow: hidden; /* 隐藏超出容器的文本 */
              text-overflow: ellipsis;
            }

            .recommend-video-detail {
              margin-top: 5px;
              display: flex;

              .recommend-video-other-info {
                font-size: 14px;
                color: #9499a0;
              }
            }

            .recommend-video-publisher {
              font-size: 14px;
              color: #9499a0;
            }
          }
        }
      }
    }
  }
}
</style>